package de.bbl.android.fragment.DataCenter;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import de.bbl.android.R;
import de.bbl.android.fragment.DataCenter.Playoffs.DataCenterPlayoffsFragment;
import de.bbl.android.fragment.DataCenter.Spielplan.DataCenterSpielplanFragment;
import de.bbl.android.fragment.DataCenter.Stats.DataCenterStatsFragment;
import de.bbl.android.fragment.DataCenter.Table.DataCenterTableFragment;

public class DataCenterFragment extends Fragment {

	Activity activity;
	String tag = "";
	boolean mDualPane;
	int mCurCheckPosition = -1;
	Fragment currentFragment = null;
	boolean isDetached = false;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.table_layout_datacenter, container, false);
        isDetached = false;
        RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.griditem1);
        rl.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDataExpanded(0);
				// DataCenterTableOnClick(v);
			}
		});
        rl = (RelativeLayout) view.findViewById(R.id.griditem2);
        rl.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDataExpanded(1);
				// DataCenterSpielplanOnClick(v);
			}
		});
        rl = (RelativeLayout) view.findViewById(R.id.griditem3);
        rl.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDataExpanded(2);
				// DataCenterStatsOnClick(v);
			}
		});
        rl = (RelativeLayout) view.findViewById(R.id.griditem4);
        rl.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDataExpanded(3);
				// DataCenterPlayoffsOnClick(v);
			}
		});
        tag = getTag();
        return view;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    	activity = getActivity();
        super.onActivityCreated(savedInstanceState);

        View dataExpanded = getActivity().findViewById(R.id.fragment_side_content);
        mDualPane = dataExpanded != null && dataExpanded.getVisibility() == View.VISIBLE;
        
        if (savedInstanceState != null) {
        	mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }
        
        if (mDualPane) {
        	mCurCheckPosition = 0;
          	showDataExpanded(mCurCheckPosition);
        }

    }

    @Deprecated
    public void DataCenterTableOnClick(View view) {
    	showDataExpanded(0);
    	/*
    	final FragmentManager  fm = getFragmentManager();
    	FragmentTransaction ft = fm.beginTransaction();
        // ft.replace(R.id.fragment_content, new DataCenterTableFragment()).addToBackStack(getTag());
    	DataCenterTableFragment dctf = new DataCenterTableFragment();
    	ft.replace(R.id.fragment_content, dctf, "datacenter-table");
    	ft.addToBackStack(getTag());
    	ft.commit();
    	tag = dctf.getTag();
    	*/
    	
    }

    @Deprecated
    public void DataCenterSpielplanOnClick(View view) {
    	showDataExpanded(1);
    	/*
    	final FragmentManager  fm = getFragmentManager();
    	FragmentTransaction ft = fm.beginTransaction();
    	DataCenterSpielplanFragment dcsf = new DataCenterSpielplanFragment();
    	ft.replace(R.id.fragment_content, dcsf, "datacenter-spielplan");
    	ft.addToBackStack(getTag());
    	ft.commit();
    	tag = dcsf.getTag();
    	*/
    }
    
    private void showDataExpanded(int currentPos) {
    	mCurCheckPosition = currentPos;
    	if (mDualPane && ! isDetached) 
    	{
	    	final FragmentManager  fm = getFragmentManager();
	    	FragmentTransaction ft = fm.beginTransaction();
	        // ft.replace(R.id.fragment_content, new DataCenterTableFragment()).addToBackStack(getTag());
	    	if (currentPos == 0) {
		    	DataCenterTableFragment dctf = new DataCenterTableFragment();
		    	currentFragment = (Fragment) dctf;
		    	ft.replace(R.id.fragment_side_content, dctf, "datacenter-table-extended");
	    	} else if (currentPos == 1) {
	        	DataCenterSpielplanFragment dcsf = new DataCenterSpielplanFragment();
	        	ft.replace(R.id.fragment_side_content, dcsf, "datacenter-spielplan-extended");
		    	currentFragment = (Fragment) dcsf;
	    	} else if (currentPos == 2) {
	        	DataCenterStatsFragment dcsf = new DataCenterStatsFragment();
	        	ft.replace(R.id.fragment_side_content, dcsf, "datacenter-stats-extended");
		    	currentFragment = (Fragment) dcsf;
	    	} else if (currentPos == 3) {
	        	DataCenterPlayoffsFragment dcpf = new DataCenterPlayoffsFragment();
	        	ft.replace(R.id.fragment_side_content, dcpf, "datacenter-playoffs-extended");
		    	currentFragment = (Fragment) dcpf;
	    	}
	    	ft.commit();
    	} else if (! isDetached) {
	    	final FragmentManager  fm = getFragmentManager();
	    	FragmentTransaction ft = fm.beginTransaction();
	        // ft.replace(R.id.fragment_content, new DataCenterTableFragment()).addToBackStack(getTag());
	    	if (currentPos == 0) {
		    	DataCenterTableFragment dctf = new DataCenterTableFragment();
		    	ft.replace(R.id.fragment_content, dctf, "datacenter-table");
		    	tag = dctf.getTag();
	    	} else if (currentPos == 1) {
	        	DataCenterSpielplanFragment dcsf = new DataCenterSpielplanFragment();
	        	ft.replace(R.id.fragment_content, dcsf, "datacenter-spielplan");
		    	tag = dcsf.getTag();
	    	} else if (currentPos == 2) {
	        	DataCenterStatsFragment dcsf = new DataCenterStatsFragment();
	        	ft.replace(R.id.fragment_content, dcsf, "datacenter-stats");
		    	tag = dcsf.getTag();
	    	} else if (currentPos == 3) {
	        	DataCenterPlayoffsFragment dcpf = new DataCenterPlayoffsFragment();
	        	ft.replace(R.id.fragment_content, dcpf, "datacenter-playoffs");
		    	tag = dcpf.getTag();
	    	}

	    	ft.addToBackStack(getTag());
	    	ft.commit();
    	}
    }

    @Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
    	inflater.inflate(R.menu.activity_bblmain, menu);
    	
    	// remove Refresh from OptionsMenu cause is implemented in titlebar
    	if (android.os.Build.VERSION.SDK_INT < 11)
    		menu.getItem(0).setVisible(false);
	}

	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
	}
	
	public String getCurrentTag() {
		return tag;
	}

    public void OpenOptionsMenu() {
    	activity.openOptionsMenu();
    }

    public void RefreshData() {
    	if (mDualPane) {
	    	if (tag.contains("table")) {
	    		( (DataCenterTableFragment)getFragmentManager().findFragmentByTag("datacenter-table-extended") ).RefreshData();
	    	} else if (tag.contains("spielplan")) {
	    		( (DataCenterSpielplanFragment)getFragmentManager().findFragmentByTag("datacenter-spielplan-extended") ).RefreshData();
	    	}
    	} else {
	    	if (tag.contains("table")) {
	    		( (DataCenterTableFragment)getFragmentManager().findFragmentByTag("datacenter-table") ).RefreshData();
	    	} else if (tag.contains("spielplan")) {
	    		( (DataCenterSpielplanFragment)getFragmentManager().findFragmentByTag("datacenter-spielplan") ).RefreshData();
	    	}
    	}
    }
    
    @Override
    public void onDestroyView() {
    	isDetached = true;
		super.onDestroyView();
    }

    @Override
	public void onDetach() {
    	isDetached = true;
		super.onDetach();
	}

}
